package com.digiwin.dap.middleware.auth.impl;


import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.digiwin.dap.middleware.auth.AbstractTokenService;
import com.digiwin.dap.middleware.auth.AuthoredSys;
import com.digiwin.dap.middleware.auth.JwtAppTokenService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author chenxsa
 * 用户token服务，暂不加上缓存
 */
@Service
public class JwtAppTokenServiceImpl extends AbstractTokenService<AuthoredSys> implements JwtAppTokenService {


    /**
     * 是否过期
     * @return
     */
    @Override
    protected  boolean isExpire(){
        return  false;
    }

    /**
     * 是否校验数据
     * @return
     */
    @Override
    protected  boolean isVerifyData(){
        return  false;
    }
    /**
     * 获取数据类型
     * @return
     */
    @Override
    protected  Class<?> getType(){
        return  AuthoredSys.class;
    }
    /**
     * 获取签名方
     *
     * @return
     */
    @Override
    protected String getIssuer() {
        return "AppToken";
    }


    /**
     * 验证数据是否合法
     *
     * @param data
     * @return
     */
    @Override
    public boolean validateData(AuthoredSys data) {
         return  data!=null && data.getSid() !=0 && StringUtils.isEmpty( data.getId());
    }

    /**
     * 根据token获取sys
     *
     * @param token
     * @return
     */

    @Override
    public AuthoredSys getData(String token) {
        DecodedJWT jwt = JWT.decode(token);
        String id= jwt.getClaim("id").asString();
        long sid= jwt.getClaim("sid").asLong();
        AuthoredSys sys=new AuthoredSys();
        sys.setId(id);
        sys.setSid(sid);
        return  sys;
    }


}
